const designWidth = 375
enum adaptScaleEnum {
  TypeScale = 'scale'
}
const scaleMethod: {[key: string]: (adaptWindow: Window) => void} = {
  scale: (adaptWindow: Window) => setScaleAdaptiveApp(adaptWindow)
}
/**
 * 设置移动端自适应代码 (通过设置屏幕百分比方案)
 */
function setScaleAdaptiveApp (adaptWindow: Window): void {
  const width = designWidth // 设计稿宽度
  const scale = adaptWindow.screen.width / width
  let meta = adaptWindow.document.querySelector('meta[name=viewport]')
  const content = `width=${width}, initial-scale=${scale}, user-scalable=no`
  if (!meta) {
    meta = adaptWindow.document.createElement('meta')
    meta.setAttribute('name', 'viewport')
    adaptWindow.document.head.appendChild(meta)
  }
  meta.setAttribute('content', content)
}

/**
 * 设置自适应窗口
 * @param adaptWindow 要设置自适应的窗体
 * @param adaptType 自适应的方案
 */
function setWindowAdapt (adaptWindow : Window, adaptType: adaptScaleEnum = adaptScaleEnum.TypeScale) {
  console.log(scaleMethod)
  scaleMethod[adaptType](adaptWindow)
  // 首先注册移动端自适应代码
  adaptWindow.addEventListener('resize', () => scaleMethod[adaptType](adaptWindow))
}

export default {
  setWindowAdapt, adaptScaleEnum
}
